<template>
  <div>
    <el-card shadow="hover" body-style="width:100%;" style="width: 100%;height: 100px;margin-bottom: 30px;">
    <el-row style="margin-bottom: 30px;height: 50px;border-radius: 10px;">
      <el-col :span="5" style="display:flex; justify-content:center; align-items:center;">
        <span style="margin-left:1px;width: 120px;font-weight:bold;color:gray;margin-top: 10px;margin-left: 5px;">用户名称</span>
        <el-input v-model="comment.username" placeholder="请输入用户名称" style="margin-top: 10px;" suffix-icon="el-icon-search" />
      </el-col>
      <el-col :span="10" style="display:flex; justify-content:center; align-items:center;">
        <span style="margin-left:1px;width: 90px;font-weight:bold;color:gray;margin-left: 50px;margin-top: 10px;">选择日期</span>
        <div class="block" style="margin-top: 10px;">
          <el-date-picker
            v-model="comment.createDate"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            format="yyyy-MM-dd HH:MM:SS"
            value-format="yyyy-MM-dd HH:MM:SS"
          />
        </div>
      </el-col>
      <el-col :span="8" style="display:flex; justify-content:right; align-items:center;margin-top: 10px;">
        <el-button type="primary" icon="el-icon-search" plain style="background-color: #0d8d84; color: #fff;" @click="getCommentList">查询</el-button>
        <el-button type="info" icon="el-icon-refresh" plain @click="reset">重置</el-button>
      </el-col>
    </el-row>
    </el-card>
    <el-card shadow="hover" body-style="width:100%;">
    <el-table :data="commentList" style="width: 100%" border stripe :header-cell-class-name="'headerBg'">
      <el-table-column prop="id" label="评论编号" width="100px" align="center">
        <template scope="scope">
          <span>{{ (pageParams.page - 1) * pageParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="name"
        label="用户名字"
        width="width"
        align="center"
      />
      <el-table-column prop="content" label="评论内容" width="width" align="center" />
      <el-table-column prop="reply" label="我的回复" width="width" align="center">
        <template slot-scope="{ row }">
          <span v-if="row.reply" style="display: flex; justify-content: center; align-items: center;">
            {{ row.reply }}
          </span>
          <span v-else style="display: flex; justify-content: center; align-items: center;">
            <i class="el-icon-chat-line-round" style="display: inline-block;" />
          </span>
        </template>
      </el-table-column>
      <el-table-column prop="createDate" label="评论时间" width="width" align="center">
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span style="margin-left: 10px">{{ scope.row.createDate }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="审核状态" width="100px" align="center">
        <template slot-scope="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="isReply" label="操作" width="width" align="center">
        <template slot-scope="scope">
          <template v-if="!scope.row.isReply && scope.row.status === '0' ">
            <el-button type="primary" size="mini" @click="saveComment(scope.row.id)">回复</el-button>
          </template>
          <template v-else>
            <el-button disabled size="mini" type="info" plain>已回复</el-button>
            <el-button v-if="scope.row.status === '0'" size="mini" @click="showAudit(scope.row.id)">申请审核</el-button>
            <el-button v-if="scope.row.status === '2'" size="mini" @click="showDelete(scope.row)">删除</el-button>
          </template>
        </template>
      </el-table-column>

    </el-table>
    <!-- 分页器 -->
    <el-pagination
      style="margin-top: 20px; textalign: center"
      :current-page="pageParams.page"
      :page-sizes="[3, 5, 10]"
      :page-size="pageParams.pageSize"
      :pager-count="4"
      layout="prev, pager, next, jumper,->,sizes,total"
      :total="pageParams.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    </el-card>
  </div>
</template>

<script>
import { deleteReply, reqAudit } from '@/api/comment/comment'
import * as comment from '@/api/comment/comment'

export default {
  name: 'CommentList',
  data() {
    return {
      commentList: [],
      // 分页参数
      pageParams: {
        page: 1,
        pageSize: 10,
        total: 0
      },
      comment: {
        username: '',
        createDate: null
      },
      startTime: null,
      endTime: null
    }
  },

  mounted() {
    this.getCommentList()
  },
  methods: {
    async showAudit(id) {
      const comment = {
        id: id,
        status: 1
      }
      const res = await this.$API.comment.reqAudit(comment)
      if (res.code === 200) {
        this.$notify({
          title: '提示',
          message: '提交评论审核成功!!!',
          type: 'success'
        })
        this.getCommentList()
      }
    },
    getStatusType(status) {
      return status === '0' ? 'warning'
        : status === '1' ? 'info'
          : status === '2' ? 'success'
            : 'danger'
    },
    getStatusText(status) {
      return status === '0' ? '未审核'
        : status === '1' ? '审核中'
          : status === '2' ? '审核成功'
            : '审核失败'
    },
    reset() {
      this.comment.createDate = []
      this.getCommentList()
    },
    // 分页函数
    handleSizeChange(newPageSize) {
      this.pageParams.pageSize = newPageSize
      this.getCommentList()
    },
    handleCurrentChange(current) {
      this.pageParams.page = current
      this.getCommentList()
    },
    async getCommentList() {
      if (this.comment.createDate != null) {
        this.startTime = this.comment.createDate[0]
        this.endTime = this.comment.createDate[1]
      }
      console.log(this.startTime)
      console.log(this.endTime)
      const res = await this.$API.comment.reqCommentListByPage(this.pageParams, this.comment, this.startTime, this.endTime)
      if (res.code === 200) {
        this.pageParams.total = res.data.total
        this.commentList = res.data.commentVoList
      }
    },

    saveComment(id) {
      this.$prompt('请输入评论内容，提交评论后无法修改', '回复评论', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputPattern: /^[\s\S]*.*[^\s][\s\S]*$/,
        inputErrorMessage: '回复内容不能为空'
      })
        .then(async({ value: reply }) => { // 解构赋值value
          const comment = { id, reply }
          console.log(comment)
          const res = await this.$API.comment.reqReply(comment)
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '回复评论成功'
            })
            this.getCommentList()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '你取消了回复'
          })
        })
    },

    // 删除这条信息
    async showDelete(row) {
      this.$confirm('删除这条评论后无法回撤, 是否继续?', '删除评论', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await this.$API.comment.deleteReply(row)
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.getCommentList()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
          this.getCommentList()
        })
    }
  }
}
</script>

<style scoped>
.headerBg {
  background: #eee!important;
}
.header-row {
  background-color: #ff5500;
  color: #fff;
}
.el-tag--warning {
  color: #fff;
  background-color: #f90;
}
.el-tag--info {
  color: #fff;
  background-color: #1890ff;
}
.el-tag--success {
  color: #fff;
  background-color: #52c41a;
}
.el-tag--danger {
  color: #fff;
  background-color: #f5222d;
}

</style>
